package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dao.CategoryDao;
import com.itheima.reggie.dao.EmployeeDao;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.ICategoryService;
import com.itheima.reggie.service.IDishService;
import com.itheima.reggie.service.IEmployeeService;
import com.itheima.reggie.service.ISetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.invoke.SwitchPoint;

@Service
public class CategoryServiceImpl
        extends ServiceImpl<CategoryDao, Category>
        implements ICategoryService {


    @Autowired
    IDishService dishService;

    @Autowired
    ISetmealService setmealService;
    /**
     * 根据id删除分类，删除之前需要进行判断
     */
    @Override
    public boolean rmeById(Long id) {

        //判断当前分类的类型
        //1.菜品分类
        //1.1查询dish表，select count(*)from dish where category_id=?
        //1.2数量大于0，返回false
        //构建条件查询构造器LambdaQueryWrapper
        LambdaQueryWrapper<Dish> dishqueryWrapper = new LambdaQueryWrapper<>();
      //添加查询条件，根据分类id查询
        dishqueryWrapper.eq(Dish::getCategoryId,id);
        int count1 =dishService.count(dishqueryWrapper);
        if (count1>0){
            return false;
        }
        //2.套餐分类
        //1.1查询setmeal表，select count(*)from setmeal where category_id=?
        //1.2数量大于0，返回false
        //构建条件查询构造器LambdaQueryWrapper
        LambdaQueryWrapper<Setmeal> setmealqueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据分类id查询
        setmealqueryWrapper.eq(Setmeal::getCategoryId,id);
        int count2 =setmealService.count(setmealqueryWrapper);
        if (count2>0){
            return false;
        }

        //3.执行删除操作
        //delete from category where id=?
        super.removeById(id);
        return true;
    }
}
